# management/models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import timedelta


class UserControl(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    task_count = models.PositiveIntegerField(default=0)
    last_activity = models.DateTimeField(null=True, blank=True)
    is_computing = models.BooleanField(default=False)  # 新增字段
    next_task_time = models.DateTimeField(null=True, blank=True)  # 新增字段，表示下次任务的预计执行时间

    def is_offline(self):
        """检查用户是否离线（活跃时间超过30分钟视为离线）"""
        if self.last_activity:
            return timezone.now() - self.last_activity > timedelta(minutes=30)
        return True  # 若无记录，视为离线
